Page({
  data: {
    backgroundArr: [
      "/images/banner/menubanner1.jpg",
      "/images/banner/menubanner2.jpg",
      "/images/banner/menubanner3.jpg",
    ],
    imgHeight: 520,
    arr: [],
    leftNum: 0,
    // 左侧ID
    leftId: 0,
    // 右侧ID
    rightId: 0,
    //box盒子高度数组
    boxheight: [],
  },
  // 左侧点击
  leftClick(e) {
    this.setData({
      leftNum: e.currentTarget.dataset.myindex,
      leftId: e.currentTarget.dataset.myindex,
      rightId: e.currentTarget.dataset.myindex
    })
  },
  // 右侧滚动
  rightScroll(e) {
    let st = e.detail.scrollTop
    let boxarray = this.data.boxheight
    let _this = this
    for (var i = 0; i < boxarray.length; i++) {
      if (st >= boxarray[i] - 10 && st < boxarray[i + 1] - 10) {
        _this.setData({
          leftNum: i,
          leftId: i
        })
        return;
      }
    }
  },
  //onLoad的生命周期 
  onLoad: function () {
    wx.cloud.callFunction({
      name: "getmenuData"
    }).then(res => {
      this.setData({
        arr: res.result.data[0].arr
      })
      this.computedHeight();
    })
  },
  // 生命周期onReady(类似于mounted)
  computedHeight: function () {
    const _this = this;
    setTimeout(() => {
      //专门用来储存高度的
      let heightArr = [0]
      // 一开始的初始值
      let baseNum = 0
      const query = wx.createSelectorQuery()
      query.selectAll('.box').boundingClientRect()
      query.selectViewport().scrollOffset()
      query.exec(function (res) {
        res[0].map(va => {
          baseNum += va.height
          heightArr.push(baseNum)
        })
        _this.setData({
          boxheight: heightArr
        })
      })
    }, 600);
  }
})